package de.pyranja.storage.spring;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import de.pyranja.storage.AsyncBinaryStorage;
import de.pyranja.storage.BinaryStorage;
import de.pyranja.storage.support.ActionFactory;

@Configuration
@Import(CoreConfig.class)
public class AsyncStorageConfig {

	@Autowired CoreConfig core;
	
	@Bean
	public ExecutorService storageDispatcher() {
		// is shut down by the storage
		return Executors.newSingleThreadExecutor();
	}
	
	@Bean(destroyMethod="shutdown")
	public BinaryStorage asyncBinaryStorage() throws NoSuchAlgorithmException, IOException {
		ActionFactory factory = 
				new ActionFactory(core.archive(), core.storage());
		return new AsyncBinaryStorage(storageDispatcher(), core.staging(), 
				core.storage(), factory);
	}
}
